package com.xxl.baselib.baseui.widget;

/**
 * package com.etransfar.uilib.view
 * title:  RoundButton
 * description:   (用一句话描述该文件做什么)
 * Copyright (c) 传化公路港物流有限公司版权所有
 * Create DateTime: 2017/6/13 18:27
 *
 * @author xuelian.xiong 2017/6/13
 */

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;

import com.xxl.baselib.R;


public class RoundButton extends Button
{
    private Context mContext;
    public static int[]	mNormalState		= new int[] {};
    public static int[]	mPressState			= new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
    public static int[]	mDisableState		= new int[] { -android.R.attr.state_enabled };
    public static int[]	mSelectedState		= new int[] { android.R.attr.state_selected, android.R.attr.state_enabled };
    private float			mRadius				= 0;																			//默认的圆角半径

    //默认文字和背景颜色
    private int			mBgNormalColor		= Color.RED;
    private int			mBgPressedColor	= Color.GREEN;
    private int			mBgDisableColor	= Color.DKGRAY;
    private int			mTextNormalColor	= Color.WHITE;
    private int			mTextPressedColor	= Color.GRAY;

    public RoundButton(Context context)
    {
        super(context);
        mContext= context;
        initUI();
    }

    public RoundButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext= context;
        getAttrs(attrs);
        initUI();
    }

    public RoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext= context;
        getAttrs(attrs);
        initUI();
    }

    private void getAttrs(AttributeSet attrs){
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs , R.styleable.RoundButtonAttr);

        mBgNormalColor = typedArray.getColor(R.styleable.RoundButtonAttr_normalBackground, Color.RED);
        mBgPressedColor = typedArray.getColor(R.styleable.RoundButtonAttr_pressedBackground, Color.GREEN);
        mBgDisableColor = typedArray.getColor(R.styleable.RoundButtonAttr_disableBackground, Color.DKGRAY);

        mTextNormalColor = typedArray.getColor(R.styleable.RoundButtonAttr_textNormalColor, Color.WHITE);
        mTextPressedColor = typedArray.getColor(R.styleable.RoundButtonAttr_textPressedColor, Color.GRAY);

        mRadius = typedArray.getDimension(R.styleable.RoundButtonAttr_radius , 0);

        typedArray.recycle();
    }

    private void initUI()
    {
        setGravity(Gravity.CENTER);
        buildDraweableState();
        buildColorDrawableState();
    }

    /**
     * 构建图片drawble
     */
    private void buildColorDrawableState()
    {
        ColorStateList colorStateList = new ColorStateList(new int[][] { mPressState, mNormalState },
                new int[] { mTextPressedColor, mTextNormalColor });
        setTextColor(colorStateList);
    }

    /**
     * 构建背景Drawble
     */
    private void buildDraweableState()
    {

        float outRectr[] = new float[] { mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius };
        //创建状态管理器
        StateListDrawable drawable = new StateListDrawable();
        /**
         * 注意StateListDrawable的构造方法我们这里使用的
         * 是第一参数它是一个float的数组保存的是圆角的半径，它是按照top-left顺时针保存的八个值
         */
        //创建圆弧形状
        RoundRectShape rectShape = new RoundRectShape(outRectr, null, null);
        //创建drawable
        ShapeDrawable pressedDrawable = new ShapeDrawable(rectShape);
        //设置我们按钮背景的颜色
        pressedDrawable.getPaint().setColor(mBgPressedColor);
        //添加到状态管理里面
        drawable.addState(mPressState, pressedDrawable);

        ShapeDrawable disableDrawable = new ShapeDrawable(rectShape);
        disableDrawable.getPaint().setColor(mBgDisableColor);
        //disableDrawable.getPaint().setAlpha(125);
        drawable.addState(mDisableState, disableDrawable);

        ShapeDrawable normalDrawable = new ShapeDrawable(rectShape);
        normalDrawable.getPaint().setColor(mBgNormalColor);
        drawable.addState(mNormalState, normalDrawable);
        //设置我们的背景，就是xml里面的selector
        setBackgroundDrawable(drawable);
    }

    /**
     * 设置圆角矩形
     *
     * @param radius
     */
    public void setRadius(int radius)
    {
        this.mRadius = radius;
        buildDraweableState();
    }

    /**
     * 设置按钮背景颜色
     *
     * @param normalColor
     * @param prssedClor
     */
    public void setBgNormalPressedcolor(int normalColor, int prssedClor)
    {

        mBgNormalColor = normalColor;
        mBgPressedColor = prssedClor;
        buildDraweableState();

    }

    /**
     * 设置按钮文字颜色
     *
     * @param normalColor
     * @param pressedColor
     */
    public void setTextNormalPressedcolor(int normalColor, int pressedColor)
    {
        mTextPressedColor = pressedColor;
        mTextNormalColor = normalColor;
        buildColorDrawableState();

    }
}